package cn.tedu._07vehicle.base.interceptors;

import cn.tedu._07vehicle.pojo.vo.UserVo;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.servlet.HandlerInterceptor;
@Slf4j
public class LoginCheckInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        log.debug("进入登录状态校验的拦截器...");
        /*
        1.获取到会话的对象:HttpSession;
        2.从会话对象中获取用户标识;
        3.判断用户标识是否为null;
          3.1为null:未登录,抛出异常 或者 重定向到登录页面;
          3.2不为null:已登录,return true 放行;
         */
        HttpSession session = request.getSession();
        UserVo userVo=(UserVo)session.getAttribute("user");
        if(userVo==null){
            response.sendRedirect("/login.html");
            return false;
        }else {
            return true;
        }
    }
}
